

package ig.formato.dao;

import ig.formato.dao.abs.DAO;
import ig.formato.dto.CampoPreprocesamientoDTO;
import ig.formato.util.IGFormatoException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
/**
 * 
 * @author jsanchez
 */
public class CampoPreprocesamientoDao extends DAO {

    public CampoPreprocesamientoDao() {
    }

    public Object buscar(Integer id) throws IGFormatoException {
        try {
            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, IDINFORME, NOMBRE, CLASSE, CLASSE2, OBJETO, SEPARADORDECIMAL, FORMULAVALOR FROM CAMPO_PRE WHERE ID = ?");
            prep.setInt(1, id.intValue());
            resul = prep.executeQuery();
            if (resul.next()) {
                return getDatos(resul, new CampoPreprocesamientoDTO());
            }
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return null;
    }

    public Collection buscarPorInforme(Integer id) throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, IDINFORME, NOMBRE, CLASSE, CLASSE2, OBJETO, SEPARADORDECIMAL, FORMULAVALOR FROM CAMPO_PRE WHERE IDINFORME = ? ORDER BY ID");
            prep.setInt(1, id.intValue());
            for (resul = prep.executeQuery(); resul.next();) {
                c.add(getDatos(resul, new CampoPreprocesamientoDTO()));
            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Collection buscarTodos() throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, IDINFORME, NOMBRE, CLASSE, CLASSE2, OBJETO, SEPARADORDECIMAL, FORMULAVALOR FROM CAMPO_PRE ORDER BY ID");
            for (resul = prep.executeQuery(); resul.next();) {
                c.add(getDatos(resul, new CampoPreprocesamientoDTO()));
            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Integer guardar(Object obj) throws IGFormatoException {
        CampoPreprocesamientoDTO dto = (CampoPreprocesamientoDTO) obj;
        try {
            getConnection();
            if (dto.getId() == null) {
                prep = connectionIgFormato.prepareStatement("INSERT INTO CAMPO_PRE(IDINFORME, NOMBRE, CLASSE, CLASSE2, OBJETO, SEPARADORDECIMAL, FORMULAVALOR, ID) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
                dto.setId(getMaximoId("CAMPO_PRE"));
            } else {
                prep = connectionIgFormato.prepareStatement("UPDATE CAMPO_PRE  SET IDINFORME=?, NOMBRE=?, CLASSE=?, CLASSE2=?, OBJETO=?, SEPARADORDECIMAL=?, FORMULAVALOR=? WHERE ID=?");
            }
            setDatos(prep, dto, "IDINFORME, NOMBRE, CLASSE, CLASSE2, OBJETO, SEPARADORDECIMAL, FORMULAVALOR, ID");
            prep.executeUpdate();
            return dto.getId();
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } catch (Throwable ex) {
            throw new IGFormatoException(ex);            
        } finally {
            closeAll();
        }
    }

    public void eliminar(Integer id) throws IGFormatoException {
        try {
            Collection<CampoPreprocesamientoDTO> c=this.buscarPorInforme(id);
            FormulacionDao formulacionDao=new FormulacionDao();
            for (CampoPreprocesamientoDTO campoPreprocesamientoDTO : c) {
                formulacionDao.eliminar(campoPreprocesamientoDTO.getId());
            }            
            getConnection();
            prep = connectionIgFormato.prepareStatement("DELETE FROM CAMPO_PRE WHERE IDINFORME = ?");
            prep.setInt(1, id.intValue());
            prep.executeUpdate();

        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Object existe(Object obj) throws IGFormatoException {
        try {
            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID FROM CAMPO_PRE WHERE NOMBRE = ?");
            prep.setString(1, ((CampoPreprocesamientoDTO) obj).getNombre().trim());
            resul = prep.executeQuery();
            if (resul.next()) {
                return Integer.valueOf(resul.getInt("ID"));
            }
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return false;
    }
}
